import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class ImageWidget extends StatelessWidget {
  final String imageUrl;
  final double width;
  final double height;
  late final Widget imageWidget;

  ImageWidget({
    required this.imageUrl, required this.width, required this.height,
  }) {
    imageWidget = _buildImageWidget();
  }

  @override
  Widget build(BuildContext context) {
    return imageWidget;
  }

  Widget _buildImageWidget() {
    if (imageUrl.startsWith('http')) {
      return _buildNetworkImage();
    }

    if (imageUrl.startsWith('asset')) {
      return _buildAssetImage();
    }

    return _buildFileImage();
  }

  Widget _buildNetworkImage() {
    return Image.network(
      imageUrl,
      fit: BoxFit.cover,
      width: width,
      height: height,
      errorBuilder: (context, url, error) => _buildPlaceholder(),
    );
  }

  Widget _buildFileImage() {
    return Image.file(
      File(imageUrl),
      fit: BoxFit.cover,
      width: width,
      height: height,
      errorBuilder: (context, error, stackTrace) => _buildPlaceholder(),
    );
  }

  Widget _buildAssetImage() {
    return Image.asset(
      imageUrl,
      fit: BoxFit.cover,
      width: width,
      height: height,
      errorBuilder: (context, error, stackTrace) => _buildPlaceholder(),
    );
  }

  Widget _buildPlaceholder() {
    return Container(
      color: Colors.grey[300],
      child: Icon(Icons.image, color: Colors.grey[800], size: 60),
      alignment: Alignment.center,
    );
  }
}